<?php
 
include_once("./configure/configure.php");						//--> global var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template
include_once("utils/StringUtils.php");				//--> String utils
include_once("utils/ImageUtils.php");				//--> image utils
include_once("utils/StyleUtils.php");				//--> Style utils

include_once("CouponImpl.php");
include_once("CouponAttributeImpl.php");

include_once("CategoryImpl.php");
include_once("constant/GlobalConstant.php");


include_once("includeSmarty.php");

$id		= 0;
$page 	= $_POST['page'];

	
$cacheId = $page;

if(!$smarty->isCached('index.html', $cacheId, $cacheId)){
	$oStringUtils 	= new StringUtils();
	$oImageUtils	= new ImageUtils();
	
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	$oCAttributeImpl= new CouponAttributeImpl(DB_TAG_COUPON);
	
	$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);
	
	include_once("includeTag.php");
	include_once("includeMerchant.php");
	include_once("includeCategories.php");
	
	if(empty($page)){
		$page = 0;
	}
	
	$where 		= " WHERE status = 1 AND isFeatured = 1";
	$orderStr	= " ORDER BY startDate DESC, r_sortValue DESC";
	
	$oCoupons = $oCouponImpl->getCustoms($where, $orderStr, $page, 20);
	
	foreach ($oCoupons as $oCoupon) {
		$oCouponAttribute = $oCAttributeImpl->getLists($oCoupon->getId());
		$oCoupon->setAttributes($oCouponAttribute);
		
		$oCategory 	= $oCategoryImpl->getByCouponId($oCoupon->getId());
		if(!is_object($oCategory))
			$oCategory = new CategoryDomain();
			
		$oCoupon->setCategory($oCategory);
		
		$oMerchant 	= $oMerchantImpl->getById($oCoupon->getMerchantId());
		
		if($oCoupon->getCreativeType() == 1){
			$oImage = $oCouponImpl->getCouponImage($oCoupon->getId());
			
			if(is_object($oImage)){
				$oImageUtils->calculateNewSize($oImage->getWidth(), $oImage->getHeight(), 80, 80, 30);
			
				$oImage->setWidth($oImageUtils->getWidth());
				$oImage->setHeight($oImageUtils->getHeight());
				
				$oCoupon->setCouponImage($oImage);
			}else{
				$oCoupon->setCreativeType(2);
			}
		}

		if($oCoupon->getCreativeType() == 2){
			$oLogo = $oMerchantImpl->getLogo($oCoupon->getMerchantId());
			
			if(!is_object($oLogo)){
				$oLogo = new ImageDomain();
				
				$oLogo->setId(0);
				$oLogo->setExtName('gif');	
			}
			
			$oMerchant->setLogo($oLogo);
		}			
		
		$oCoupon->setMerchant($oMerchant);
	}
	
	
	$smarty->assign('oMerchants', $oMerchants);
	$smarty->assign('oTags', $oTags);
	$smarty->assign('oCoupons', $oCoupons);
	$smarty->assign('oCategories', $oCategories);
	
	$smarty->assign('pageList', $oCouponImpl->getPageList(10));
	$smarty->assign('nextPage', $oCouponImpl->getNextPage());
	$smarty->assign('prePage', $oCouponImpl->getPrePage());
	$smarty->assign('pageCount', $oCouponImpl->getPageCount());
	$smarty->assign('page', $oCouponImpl->getPage());
	
	$smarty->assign('id', $id);
	
}

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL_HTTP);

$smarty->assign('theme', StyleUtils::getTheme());
$smarty->assign('StoreBoxTitle', "Popular Stores");


$smarty->display('index.html', $cacheId, $cacheId);
?>
